//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.commons.plugins.mybatisPagePlugs;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class ReflectHelper {
    private static final Logger logger = LoggerFactory.getLogger(ReflectHelper.class);

    public ReflectHelper() {
    }

    public static Field getFieldByFieldName(Object obj, String fieldName) {
        Class superClass = obj.getClass();

        while(superClass != Object.class) {
            try {
                return superClass.getDeclaredField(fieldName);
            } catch (NoSuchFieldException var4) {
                superClass = superClass.getSuperclass();
            }
        }

        return null;
    }

    public static Map<String, Object> setBeanValueToMap(Object bean) throws IllegalArgumentException, IllegalAccessException {
        HashMap params = new HashMap();
        if(bean != null) {
            Field[] fields = bean.getClass().getDeclaredFields();
            Object beanValue = null;
            Field[] var4 = fields;
            int var5 = fields.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                Field field = var4[var6];
                String fieldName = field.getName();
                if(field.isAccessible()) {
                    beanValue = field.get(bean);
                } else {
                    field.setAccessible(true);
                    beanValue = field.get(bean);
                }

                params.put(fieldName, beanValue);
            }
        }

        return params;
    }

    public static Object getValueByFieldName(Object obj, String fieldName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Field field = getFieldByFieldName(obj, fieldName);
        Object value = null;
        if(field != null) {
            if(field.isAccessible()) {
                value = field.get(obj);
            } else {
                field.setAccessible(true);
                value = field.get(obj);
                field.setAccessible(false);
            }
        }

        return value;
    }

    public static void setValueByFieldName(Object obj, String fieldName, Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        Field field = obj.getClass().getDeclaredField(fieldName);
        if(field.isAccessible()) {
            field.set(obj, value);
        } else {
            field.setAccessible(true);
            field.set(obj, value);
            field.setAccessible(false);
        }

    }

    public static void setBeanToRequest(HttpServletRequest request, Object bean) {
        try {
            Class e = bean.getClass();
            Field[] fields = e.getDeclaredFields();
            Field[] var4 = fields;
            int var5 = fields.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                Field field = var4[var6];
                String property = field.getName();
                String methodName = property.substring(0, 1).toUpperCase() + property.substring(1);
                Method m = e.getMethod("get" + methodName, new Class[0]);
                Object arg = null;
                m.invoke(bean, (Object[])arg);
            }
        } catch (Exception var13) {
            logger.error("", var13);
        }

        request.setAttribute(bean.getClass().getSimpleName(), bean);
    }

    public static void getReuqestValueSetBean(Object bean, HttpServletRequest request) {
        if(bean != null) {
            try {
                PropertyDescriptor[] e = PropertyUtils.getPropertyDescriptors(bean);

                for(int i = 0; i < e.length; ++i) {
                    PropertyDescriptor pd = e[i];
                    Method method = PropertyUtils.getWriteMethod(pd);
                    if(method != null) {
                        Object value = request.getParameter(pd.getName());
                        if(value != null) {
                            if(pd.getPropertyType().equals(String.class)) {
                                BeanUtils.setProperty(bean, pd.getName(), value);
                            } else if(pd.getPropertyType() != Integer.class && pd.getPropertyType() != Integer.TYPE) {
                                if(pd.getPropertyType() != Long.class && pd.getPropertyType() != Long.TYPE) {
                                    if(pd.getPropertyType() == Double.class || pd.getPropertyType() == Double.TYPE) {
                                        if("".equals(value)) {
                                            if(pd.getPropertyType() == Double.TYPE) {
                                                value = new Double(0.0D);
                                            } else {
                                                value = null;
                                            }
                                        }

                                        BeanUtils.setProperty(bean, pd.getName(), value);
                                    }
                                } else {
                                    if("".equals(value)) {
                                        if(pd.getPropertyType() == Long.TYPE) {
                                            value = new Long(0L);
                                        } else {
                                            value = null;
                                        }
                                    }

                                    BeanUtils.setProperty(bean, pd.getName(), value);
                                }
                            } else {
                                if("".equals(value)) {
                                    if(pd.getPropertyType() == Integer.TYPE) {
                                        value = new Integer(0);
                                    } else {
                                        value = null;
                                    }
                                }

                                BeanUtils.setProperty(bean, pd.getName(), value);
                            }
                        }
                    }
                }
            } catch (IllegalAccessException var7) {
                logger.error("", var7);
            } catch (InvocationTargetException var8) {
                logger.error("", var8);
            }

        }
    }
}
